package com.example.heimaplayer.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.heimaplayer.R
import com.example.heimaplayer.model.HomeItemBean
import com.example.heimaplayer.ui.widget.LoadMoreView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.item_home.view.*
import org.jetbrains.anko.find
import org.jetbrains.anko.runOnUiThread
import org.jetbrains.anko.toast
import widget.HomeItenView

class HomeAdapter: RecyclerView.Adapter<HomeAdapter.HomeHolder>() {

    private var list =  ArrayList<HomeItemBean>()
//      var list : MutableList<HomeItemBean> = ArrayList()
//处理接口中的数据 数据丢进去
    class HomeHolder(view:View):RecyclerView.ViewHolder(view){

//         var img : ImageView = view.findViewById(R.id.img_bg)
//         val text : TextView = view.findViewById(R.id.tv_title)
//         val tv_desc : TextView = view.findViewById(R.id.tv_desc)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeHolder {
//        return HomeHolder(HomeItenView(parent?.context))
        if(viewType==1){
            return  HomeHolder(LoadMoreView(parent.context))
        }else{
            return HomeHolder(HomeItenView(parent.context))
        }
//        if (viewType == 1){
//            //最后一条
//            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home,parent,false)
//            return HomeHolder(LoadMoreView(parent?.context))
//        }else{
//            //普通条目
//            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home,parent,false)
//            return HomeHolder(view)
//        }

//        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home,parent,false)
//        return HomeHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size+1

    }

    override fun onBindViewHolder(holder: HomeHolder, position: Int) {
//            holder.text.setText(list.get(position).company)
//        数据导入，列表展示
        if (position == list.size)return
        val data = list.get(position)
        val itenView = holder.itemView as HomeItenView
        itenView.setData(data)
//        if (position == list.size){
//            holder.text?.setText("")
//            holder.tv_desc?.setText("")
//
//        }else{
//            println(list)
//            holder.text?.setText(list?.get(position).title)
//            holder.tv_desc?.setText(list?.get(position).country)
//            Picasso.get().load(list?.get(position).album_500_500).into(holder.img)
//        }

    }

    override fun getItemViewType(position: Int): Int {
        if (position == list.size){
            //最后一条
            return 1
        }else{
            //普通条目
            return 0
        }
    }



    fun  setData(listx: MutableList<HomeItemBean>) {
        list.clear()
        list.addAll(listx)
        notifyDataSetChanged()
    }

    fun loadMoreData(list: List<HomeItemBean>) {
        this.list.addAll(list)
        notifyDataSetChanged()
    }

//    override fun getItemViewType(position: Int): Int {
//        if (position ==list.size){
//            return 1
//        }else
//            return 0
//    }
}


